package controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import dao.SalesOrderDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import pojo.Customer;
import pojo.SalesOrder;
import service.SalesOrderService;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/order")
public class SalesOrderController {
    @Autowired
    SalesOrderService salesOrderService;

    @PostMapping("queryOrder")
    @ResponseBody
    public SalesOrder queryOrder(@RequestBody SalesOrder salesOrder){
       return salesOrderService.queryOrder(salesOrder.getId());
    }

    @PostMapping("getAllOrder")
    @ResponseBody
    public List<SalesOrder>  getAllOrder(){
        return salesOrderService.getAllOrder();
    }

    @PostMapping("addOrder")
    @ResponseBody
    public int addOrder(@RequestBody Map<String, Object> map) throws JsonProcessingException {
        System.out.println(map);
        int customerId = (Integer) map.get("customerId");
        //map转对象
        ObjectMapper mapper = new ObjectMapper(); //转换器
        SalesOrder salesOrder=mapper.readValue( mapper.writeValueAsString(map.get("salesOrder")), SalesOrder.class);
        return salesOrderService.addOrder(salesOrder,customerId);
    }



    @PostMapping("updateOrder")
    @ResponseBody
    public int updateOrder(@RequestBody SalesOrder salesOrder){
        return salesOrderService.updateOrder(salesOrder);
    }

    @PostMapping("deleteOrder")
    @ResponseBody
    public int deleteOrder(@RequestBody SalesOrder salesOrder){
        return salesOrderService.deleteOrder(salesOrder.getId());
    }

    @GetMapping("sumOrderMoney")
    @ResponseBody
    public int sumOrderMoney(String start,String end){
        return salesOrderService.sumOrderByTime(start,end);
    }

    @GetMapping("getEndTimeOrder")
    @ResponseBody
    public List<SalesOrder> getEndTimeOrder(String start,String end){
        return salesOrderService.getOrderByEndTime(start,end);
    }







}
